package lambda;


import org.junit.Test;

import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @description: 方法引用的例子
 * @author: apple
 * @date:  2022/1/3 6:55 下午
 * @version 1.0
 **/
public class MethedRefTest {

    @Test
    public void test1() {

        // str -> System.out.println("str")为Consumer的匿名实现类对象
        Consumer<String> con = str -> System.out.println("str");
        con.accept("hello");


        // 方法引用写法:对象::非静态方法
        Consumer<String> con2 = System.out :: println;
        con2.accept("helo");
    }

    @Test
    public void test2() {
        Table t = new Table(100,"金丝楠木");
        Supplier<String> su = () -> t.getName();
        System.out.println(su.get());

        Supplier<String> su2 = t :: getName;
        System.out.println(su2.get());
    }

    @Test
    public void test3() {
        // 方法引用：类名::静态方法
        Comparator<Integer> com = (o1 , o2) -> Integer.compare(o1,o2);

        Comparator<Integer> com2 = Integer :: compare;
    }

    public static void show(String str) {
        System.out.println(str);
    }

    class Table {

        private int id;
        private String name;

        public Table(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
